#!/bin/bash
set -e

# version check: https://github.com/ImageMagick/ImageMagick/releases
IMAGE_MAGICK_VERSION="7.0.10-6"
IMAGE_MAGICK_HASH="37d36f4d736eb16e0dd43c50302e1d01d1bb1125165333df8273508a22f8a64d"

# version check: https://libpng.sourceforge.io/index.html
LIBPNG_VERSION="1.6.37"
LIBPNG_HASH="daeb2620d829575513e35fecc83f0d3791a620b9b93d800b763542ece9390fb4"

# version check: https://github.com/strukturag/libheif/releases
LIBHEIF_VERSION="1.7.0"
LIBHEIF_HASH="11645cf2536f779be82ba9c25854fb7211b0ac30458f4764f1f7de88763deb21"

PREFIX=/usr/local
WDIR=/tmp/imagemagick

# Install build deps
apt -y -q remove imagemagick
apt -y -q install ghostscript gsfonts pkg-config autoconf libbz2-dev libjpeg-dev libtiff-dev libfreetype6-dev libde265-dev

mkdir -p $WDIR
cd $WDIR

# Build and install libpng
wget -O $WDIR/libpng.tar.gz https://prdownloads.sourceforge.net/libpng/libpng-$LIBPNG_VERSION.tar.gz?download
sha256sum $WDIR/libpng.tar.gz
echo "$LIBPNG_HASH $WDIR/libpng.tar.gz" | sha256sum -c

tar -xzvf $WDIR/libpng.tar.gz
cd $WDIR/libpng-$LIBPNG_VERSION

./configure --prefix=$PREFIX
make all && make install

# Build and install libheif
cd $WDIR
wget -O $WDIR/libheif.tar.gz "https://github.com/strukturag/libheif/archive/v$LIBHEIF_VERSION.tar.gz"
sha256sum $WDIR/libheif.tar.gz
echo "$LIBHEIF_HASH $WDIR/libheif.tar.gz" | sha256sum -c
tar -xzvf $WDIR/libheif.tar.gz
cd libheif-$LIBHEIF_VERSION
./autogen.sh
./configure
make && make install

# Build and install ImageMagick
wget -O $WDIR/ImageMagick.tar.gz "https://github.com/ImageMagick/ImageMagick/archive/$IMAGE_MAGICK_VERSION.tar.gz"
sha256sum $WDIR/ImageMagick.tar.gz
echo "$IMAGE_MAGICK_HASH $WDIR/ImageMagick.tar.gz" | sha256sum -c
IMDIR=$WDIR/$(tar tzf $WDIR/ImageMagick.tar.gz --wildcards "ImageMagick-*/configure" |cut -d/ -f1)
tar zxf $WDIR/ImageMagick.tar.gz -C $WDIR
cd $IMDIR
PKG_CONF_LIBDIR=$PREFIX/lib LDFLAGS=-L$PREFIX/lib CFLAGS=-I$PREFIX/include ./configure \
          --prefix=$PREFIX \
          --enable-static \
          --enable-bounds-checking \
          --enable-hdri \
          --enable-hugepages \
          --with-threads \
          --with-modules \
          --with-quantum-depth=16 \
          --without-magick-plus-plus \
          --with-bzlib \
          --with-zlib \
          --without-autotrace \
          --with-freetype \
          --with-jpeg \
          --without-lcms \
          --with-lzma \
          --with-png \
          --with-tiff \
          --with-heic
make all && make install

cd $HOME
rm -rf $WDIR
ldconfig /usr/local/lib
